<template>
    <view class="drawer-mask" :class="{ 'drawer-mask-show': modelValue, 'dark': isDarkMode }" @tap="handleClose">
        <view class="drawer-container" :class="[
            'drawer-position-' + position,
            { 'drawer-show': modelValue, 'dark': isDarkMode }
        ]" :style="getContainerStyle" @tap.stop>
            <!-- 状态栏占位 -->
            <view v-if="position === 'top'" class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
            <!-- 标题栏 -->
            <view v-if="showHeader" class="drawer-header" :class="{ 'dark': isDarkMode, 'top': position === 'top' }">
                <text class="drawer-title" :class="{ 'dark': isDarkMode }">{{ title }}</text>
                <text class="drawer-close bigfont big-guanbi" :class="{ 'dark': isDarkMode }" @tap="handleClose"></text>
            </view>
            <!-- 内容区域 -->
            <view class="drawer-content" :class="{ 'dark': isDarkMode, 'no-header': !showHeader }">
                <slot></slot>
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { computed, defineEmits, defineProps, onMounted, ref, watch } from 'vue';
import { isDarkMode } from '../store/theme';

const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false
    },
    title: {
        type: String,
        default: ''
    },
    height: {
        type: [String, Number],
        default: '80%'
    },
    position: {
        type: String,
        default: 'bottom',
        validator: (value: string) => ['top', 'right', 'bottom', 'left'].includes(value)
    },
    showHeader: {
        type: Boolean,
        default: true
    }
});

const statusBarHeight = ref(0);

onMounted(() => {
    // 获取状态栏高度
    const systemInfo = uni.getSystemInfoSync();
    statusBarHeight.value = systemInfo.statusBarHeight || 20;
});

const emit = defineEmits(['update:modelValue', 'close', 'after-enter']);

const handleClose = () => {
    emit('update:modelValue', false);
    emit('close');
};

const getContainerStyle = computed(() => {
    const style: Record<string, string> = {};

    if (['top', 'bottom'].includes(props.position)) {
        style.height = typeof props.height === 'number' ? props.height + 'rpx' : props.height;
        style.width = '100%';
        // 如果是从顶部打开，需要加上状态栏高度
        if (props.position === 'top') {
            const totalHeight = `calc(${style.height} + ${statusBarHeight.value}px)`;
            style.height = totalHeight;
        }
    } else {
        style.width = typeof props.height === 'number' ? props.height + 'rpx' : props.height;
        style.height = '100%';
    }

    return style;
});

// 监听modelValue的变化
watch(() => props.modelValue, (newVal) => {
    if (newVal) {
        // 当抽屉打开时，等待动画结束后触发after-enter事件
        setTimeout(() => {
            emit('after-enter');
        }, 300); // 300ms是抽屉打开动画的时间
    }
});
</script>

<style lang="scss" scoped>
.drawer-mask {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.6);
    opacity: 0;
    visibility: hidden;
    z-index: 998;
    transition: all 0.3s ease;

    &.drawer-mask-show {
        opacity: 1;
        visibility: visible;
    }

    &.dark {
        background: rgba(0, 0, 0, 0.8);
    }
}

.drawer-container {
    position: fixed;
    background: #fff;
    z-index: 999;
    transition: all 0.3s ease;

    // 从底部滑入
    &.drawer-position-bottom {
        left: 0;
        bottom: -100%;
        width: 100%;
        border-radius: 24rpx 24rpx 0 0;

        &.drawer-show {
            bottom: 0;
        }
    }

    // 从顶部滑入
    &.drawer-position-top {
        left: 0;
        top: -100%;
        width: 100%;
        border-radius: 0 0 24rpx 24rpx;

        &.drawer-show {
            top: 0;
        }
    }

    // 从右侧滑入
    &.drawer-position-right {
        top: 0;
        right: -100%;
        height: 100%;
        border-radius: 24rpx 0 0 24rpx;

        &.drawer-show {
            right: 0;
        }
    }

    // 从左侧滑入
    &.drawer-position-left {
        top: 0;
        left: -100%;
        height: 100%;
        border-radius: 0 24rpx 24rpx 0;

        &.drawer-show {
            left: 0;
        }
    }

    &.dark {
        background: #1a1a1a;
    }
}

.status-bar {
    width: 100%;
    background: transparent;
}

.drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 30rpx;
    border-bottom: 1px solid #eee;
    height: 90rpx;
    box-sizing: border-box;

    &.top {
        border-top: 1px solid #eee;
        border-bottom: none;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fff;
    }

    &.dark {
        border-color: #333;

        &.top {
            background: #1a1a1a;
            border-top-color: #333;
        }
    }
}

.drawer-title {
    font-size: 32rpx;
    color: #333;

    &.dark {
        color: #fff;
    }
}

.drawer-close {
    font-size: 36rpx;
    color: #999;
    padding: 10rpx;

    &.dark {
        color: #fff;
    }
}

.drawer-content {
    height: calc(100% - 90rpx);
    overflow-y: auto;
    box-sizing: border-box;

    &.no-header {
        height: 100%;
    }

    &::-webkit-scrollbar {
        width: 6rpx;
    }

    &::-webkit-scrollbar-thumb {
        background-color: #ddd;
        border-radius: 3rpx;
    }

    &::-webkit-scrollbar-track {
        background-color: transparent;
    }

    &.dark {
        color: #fff;

        &::-webkit-scrollbar-thumb {
            background-color: #666;
        }
    }
}

:global(.dark-mode) {
    .drawer-mask {
        background: rgba(0, 0, 0, 0.8);
    }

    .drawer-container {
        background: #1a1a1a;
    }

    .drawer-header {
        border-color: #333;
    }

    .drawer-title {
        color: #fff;
    }

    .drawer-close {
        color: #fff;
    }

    .drawer-content {
        color: #fff;
    }
}
</style>